package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-09
 */
@Api(description = "会员管理")
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //查询某一天注册人数
    @ApiOperation("根据用日期查询某天注册人数【service-statics模块需要】")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day) {
        System.out.println("进入： com.atguigu.educenter.controller ");
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }

    //根据用id获取用户信息
    @ApiOperation("根据用id获取用户信息【service-order模块需要】")
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String id) {
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        UcenterMember member = memberService.getById(id);
        BeanUtils.copyProperties(member, memberOrder);
        return memberOrder;
    }

    //登录
    @ApiOperation("会员登录")
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    //注册
    @ApiOperation("会员注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @ApiOperation("通过token获取用户信息")
    @GetMapping("getMemberInfo")
    //从路径中获取 token信息
    // token是存放在请求头里面
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }


    //判断手机号是否重复
    @ApiOperation("判断手机号是否重复")
    @GetMapping("mobileExit/{mobile}")
    public R mobileExit(@PathVariable String mobile) {
        System.out.println("手机号码是：" + mobile);
        boolean exit = memberService.mobileExit(mobile);
        return R.ok().data("mobileHasExit",exit);
    }

    //修改密码
    @ApiOperation("修改密码")
    @PostMapping("modifyPassword")
    public R modifyPassword(@RequestBody RegisterVo registerVo) {
        boolean b = memberService.modifyPassword(registerVo);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //修改用户信息
    @ApiOperation("修改用户信息")
    @PostMapping("modify")
    public R modify(@RequestBody RegisterVo registerVo) {
        boolean b = memberService.modify(registerVo);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据id获取用户信息
    @ApiOperation("根据id获取用户信息")
    @GetMapping("getUcenterDetailById/{id}")
    public R getUcenterDetailById(@PathVariable("id") String id) {
        UcenterMember member = memberService.getById(id);
        return R.ok().data("getUcenterDetailById",member);
    }
}

